#include <dspSimulator/instruction/ldbu.h>

bool LDBU::checkCondition(std::shared_ptr<DspMachine> dspMachine) {
    if (isCompactInstruction) {
        return true;
    } else {
        return checkStandardCondition(dspMachine);
    }
}

void LDBU::loadInstIRIntoJit(llvm::LLVMContext *llvmContext, llvm::Module *module,
                             std::shared_ptr<llvm::IRBuilder<>> irBuilder) {

}

std::shared_ptr<Instruction> LDBU::decode(std::shared_ptr<InstInfo> instInfo, u64 baseCycle) {
    std::shared_ptr<LDBU> ldbu;
    auto dspInstInfo = std::dynamic_pointer_cast<DspInstInfo>(instInfo);
    bool isStandardInst = dspInstInfo->isStandardInstruction();
    if (isStandardInst) {
        auto data = vectorToBitSet<32>(dspInstInfo->getBits());
        if (checkBits<5>(data, 2, 0b00101) && checkBits<1>(data, 8, 0b0) && existStandardCondition(data)) {
            ldbu = std::make_shared<LDBU>(dspInstInfo->getAddress(), baseCycle);
            ldbu->creg_z = extractBits<4>(data, 28);
            ldbu->dst = extractBits<5>(data, 23).to_ulong();
            ldbu->baseR = extractBits<5>(data, 18).to_ulong();
            ldbu->offsetR = extractBits<5>(data, 13).to_ulong();
            ldbu->cst = ldbu->offsetR;
            ldbu->mode = extractBits<4>(data, 9).to_ulong();
            ldbu->y = data[7];
            if (ldbu->y == 0) {
                ldbu->funcUnit = FuncUnit::D1;
            } else {
                ldbu->funcUnit = FuncUnit::D2;
            }
            ldbu->s = data[1];
            ldbu->p = data[0];
            ldbu->instFormat = InstFormat::Kind1;
        } else if (checkBits<5>(data, 2, 0b00111) && existStandardCondition(data)) {
            ldbu = std::make_shared<LDBU>(dspInstInfo->getAddress(), baseCycle);
            ldbu->creg_z = extractBits<4>(data, 28);
            ldbu->dst = extractBits<5>(data, 23).to_ulong();
            ldbu->cst = extractBits<15>(data, 8).to_ulong();
            ldbu->y = data[7];
            if (ldbu->y == 0) {
                ldbu->funcUnit = FuncUnit::D1;
            } else {
                ldbu->funcUnit = FuncUnit::D2;
            }
            ldbu->s = data[1];
            ldbu->p = data[0];
            ldbu->instFormat = InstFormat::Kind2;
        }
    } else {
        auto data = vectorToBitSet<16>(dspInstInfo->getBits());
        if (data[10] == 0 && checkBits<2>(data, 1, 0b10) && dspInstInfo->getDSZ() == 0b000 && data[9] == 1 &&
            data[3] == 1) {
            ldbu = std::make_shared<LDBU>(dspInstInfo->getAddress(), baseCycle);
            ldbu->isCompactInstruction = true;
            ldbu->compactInstKind = "Doff4";
            ldbu->cst = extractBits<3>(data, 13).to_ulong();
            ldbu->cst += static_cast<u32>(data[11]) << 3;
            ldbu->s = data[12];     // t equal s in C8
            ldbu->baseR = extractBits<2>(data, 7).to_ulong() + 4;
            ldbu->dst = ldbu->src1 = extractBits<3>(data, 4).to_ulong();
            ldbu->mode = 0b0001;   // *+ptr[ucst4]
            ldbu->y = data[0];       // s equal y in C
            if (ldbu->y == 0) {
                ldbu->funcUnit = FuncUnit::D1;
            } else {
                ldbu->funcUnit = FuncUnit::D2;
            }
            ldbu->PROT = dspInstInfo->getPROT();
            ldbu->SAT = dspInstInfo->getSAT();
            ldbu->BR = dspInstInfo->getBR();
            ldbu->RS = dspInstInfo->getRS();
            ldbu->DSZ = dspInstInfo->getDSZ();
            ldbu->p = dspInstInfo->getP();
            ldbu->fixUpRegOnCompactInstruction();
            ldbu->instFormat = InstFormat::Kind1;
        } else {
            if (dspInstInfo->getDSZ() == 0b000 && checkBits<2>(data, 14, 0b00) && checkBits<3>(data, 9, 0b111) &&
                checkBits<3>(data, 1, 0b110)) {
                ldbu = std::make_shared<LDBU>(dspInstInfo->getAddress(), baseCycle);
                ldbu->isCompactInstruction = true;
                ldbu->compactInstKind = "Dinc";
                ldbu->cst = extractBits<1>(data, 13).to_ulong();
                ldbu->cst += 1;
                ldbu->s = data[12];     // t equal s in C
                ldbu->baseR = extractBits<2>(data, 7).to_ulong() + 4;
                ldbu->dst = extractBits<3>(data, 4).to_ulong();
                ldbu->mode = 0b1011;   //  *R++[ucst5]
                ldbu->y = data[0];       // s equal y in C
                if (ldbu->y == 0) {
                    ldbu->funcUnit = FuncUnit::D1;
                } else {
                    ldbu->funcUnit = FuncUnit::D2;
                }
                ldbu->PROT = dspInstInfo->getPROT();
                ldbu->SAT = dspInstInfo->getSAT();
                ldbu->BR = dspInstInfo->getBR();
                ldbu->RS = dspInstInfo->getRS();
                ldbu->DSZ = dspInstInfo->getDSZ();
                ldbu->p = dspInstInfo->getP();
                ldbu->fixUpRegOnCompactInstruction();
                ldbu->instFormat = InstFormat::Kind1;
            }
        }
    }
    return ldbu;
}

void LDBU::executeCustom(std::shared_ptr<TargetMachine> targetMachine, ExecutePhase executePhase) {
    auto dspMachine = std::dynamic_pointer_cast<DspMachine>(targetMachine);
    if (executePhase == ExecutePhase::E1) {
        if (instFormat == InstFormat::Kind1) {
            u32 *baseRReadPtr;
            u32 *baseRWritePtr;
            if (y == 0) {
                baseRReadPtr = dspMachine->getSaveRegs()->getReg32ByName(RegAName[baseR])->getDataPtr();
                baseRWritePtr = dspMachine->getRegisters()->getReg32ByName(RegAName[baseR])->getDataPtr();
            } else {
                baseRReadPtr = dspMachine->getSaveRegs()->getReg32ByName(RegBName[baseR])->getDataPtr();
                baseRWritePtr = dspMachine->getRegisters()->getReg32ByName(RegBName[baseR])->getDataPtr();
            }
            m_visitMemAddr = computeRealAddress(baseRReadPtr, baseRWritePtr, mode, 0, dspMachine);
        } else if (instFormat == InstFormat::Kind2) {
            u32 *baseRPtr;
            if (y == 0) {
                baseRPtr = dspMachine->getSaveRegs()->getReg32ByName(RegBName[static_cast<u32>(RegBKind::B14)])->getDataPtr();
            } else {
                baseRPtr = dspMachine->getSaveRegs()->getReg32ByName(RegBName[static_cast<u32>(RegBKind::B15)])->getDataPtr();
            }
            m_visitMemAddr = *baseRPtr + (cst << 0);
        }
    } else if (executePhase == ExecutePhase::E3) {
        m_data = dspMachine->getMMU()->getMemory()->readNBytes(m_visitMemAddr, 1);
    } else if (executePhase == ExecutePhase::E5) {
        *dstLoRegPtr = static_cast<u32>(m_data[0]);
    }
}

std::string LDBU::toString() const {
    std::string res;
    if (isCompactInstruction) {

    } else {
        res += getStandardConditionString() + " ";
    }
    res += m_name + " " + FuncUnitName[static_cast<u32>(funcUnit)] + " ";
    switch (instFormat) {
        case InstFormat::Kind1:
            res += addressModeToString() + "," + getDstReg32Name();
            break;
        case InstFormat::Kind2:
            if (y == 0) {
                res = res + " B14[" + std::to_string(cst) + "]," + getDstReg32Name();
            } else {
                res = res + " B15[" + std::to_string(cst) + "]," + getDstReg32Name();
            }
            break;
        default:
            break;
    }
    return res;
}

std::shared_ptr<DspInstruction> LDBU::clone() {
    return std::make_shared<LDBU>(*this);
}
